<?php
declare (strict_types = 1);

namespace app\tools;

use app\constant\LogicConstant;
use app\facade\StringFacade;

final class ArraysTool
{
    /**
     * 用key值替代二维数组索引
     *
     * @param $data
     * @param $key
     * @return array
     */
    public function replaceKeyByValue($data, $key)
    {
        $result = [];
        if (!empty($data)) {
            foreach ($data as $item) {
                $result[$item[$key]] = $item;
            }
        }

        return $result;
    }

    /**
     * 用key值替代二维数组索引并分组
     *
     * @param $data
     * @param $key
     * @param $key2
     * @return array
     */
    public function groupKeyByValue($data, $key, $key2)
    {
        $result = [];
        if (!empty($data)) {
            foreach ($data as $item) {
                $result[$item[$key]][] = $item[$key2];
            }
        }

        return $result;
    }

    /**
     * 双key分组
     *
     * @param $data
     * @param $key
     * @param $key2
     * @return array
     */
    public function replaceByTwoKey($data, $key, $key2)
    {
        $result = [];
        if (!empty($data)) {
            foreach ($data as $item) {
                $result[$item[$key] . '_' . $item[$key2]] = $item;
            }
        }

        return $result;
    }

    /**
     * 根据父子字段获取树状
     *
     * @param $children
     * @param int $parent
     * @param string $childColumn
     * @param string $parentColumn
     * @return array
     */
    public function getTree($children, $parent = 0, $childColumn = 'id', $parentColumn = 'pid')
    {
        $result = [];
        foreach ($children as $key => $item) {
            if ($item[$parentColumn] == $parent) {
                $item['children'] = $this->getTree($children, $item[$childColumn], $childColumn, $parentColumn);
                $result[] = $item;
            }
        }

        return $result;
    }

    /**
     * 单条记录逻辑数据构建
     *
     * @param $item
     * @return mixed
     */
    public function buildLogLogicData($item)
    {
        switch ($item['data_type'])
        {
            case LogicConstant::LOG_DATA_TYPE_ONE:
                // 文本
                switch ($item['data_logic_type']) {
                    case LogicConstant::LOG_DATA_LOGIC_TYPE_ONE:
                        // value 需要区分 key value, 插 变更为
                        $value = explode('`|`', $item['data']['value']);
                        $item['data'] = $item['data']['key'] . '：从（' . $value[0] . '）' . '变更为' . '（' . $value[1] . '）';
                        break;
                    case LogicConstant::LOG_DATA_LOGIC_TYPE_TWO:
                        $item['data'] = $item['data']['key'] . ': ' . $item['data']['value'];
                        break;
                    default:
                        $item['data'] = empty($item['data']['value']) ? '' :  $item['data']['value'];
                        break;
                }
                break;
            case LogicConstant::LOG_DATA_TYPE_TWO:
                // 多张图片
                switch ($item['data_logic_type']) {
                    case LogicConstant::LOG_DATA_LOGIC_TYPE_ZERO:
                    case LogicConstant::LOG_DATA_LOGIC_TYPE_THREE:
                        // 普通
                        if (!empty($item['data'])) {
                            $item['data']['value'] = array_map(function ($ii) {
                                return StringFacade::splicingDomainShowUrl($ii);
                            }, $item['data']['value']);
                        }
                        break;
                }
                break;
            default:
                break;
        }
        return $item;
    }

    /**
     * 单条记录逻辑数据构建(Amis用, 就是去掉样式, 不想装封)
     *
     * @param $item
     * @return mixed
     */
    public function buildLogLogicDataAmis($item)
    {
        switch ($item['data_type'])
        {
            case LogicConstant::LOG_DATA_TYPE_ONE:
                // 文本
                switch ($item['data_logic_type']) {
                    case LogicConstant::LOG_DATA_LOGIC_TYPE_ONE:
                        // value 需要区分 from to
                        $value = explode('|', $item['data']['value']);
                        $item['data'] = $item['data']['key'] . '：从（' . $value[0] . '）' . '变更为' . '（' . $value[1] . '）';
                        break;
                    default:
                        $item['data'] = empty($item['data']['key']) ?
                            $item['data']['value']
                            :
                            $item['data']['key'] . '：' . $item['data']['value'];
                        break;
                }
                break;
            default:
                break;
        }
        return $item;
    }
}
